<!-- 公告历史纪录 -->
<template>
  <div>
    <div class="btns">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="toAdd">警报添加</el-button>
        </el-form-item>
        <el-form-item>
          <el-select clearable v-model="form.status" placeholder="请选择警报状态">
            <el-option label="已解决" value="resolved"></el-option>
            <el-option label="未解决" value="unresolved"></el-option>
            <el-option label="已忽略" value="ignored"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input clearable v-model="form.alert_type" placeholder="请输入警报类型"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table :data="gData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="alert_type" label="警报类型">
        </el-table-column>
        <el-table-column prop="description" label="警报描述">
        </el-table-column>
        <el-table-column label="警报状态" align="center">
          <template v-slot="scope">
            <div>
              <el-tag
                :type="scope.row.status === 'unresolved' ? 'danger' : (scope.row.status === 'resolved' ? 'success' : 'warning')">
                {{ scope.row.status === 'unresolved' ? '未解决' : (scope.row.status === 'resolved' ? '已解决' :
                  '已忽略') }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="发表时间" align="center">
        </el-table-column>
        <el-table-column prop="updated_at" label="更新时间" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <div>
              <el-tag @click="toEdit(scope.row)" type="primary" style="margin-right:15px">警报处理</el-tag>
              <el-tag @click="toDelete(scope.row.id)" type="danger">删除</el-tag>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page" style="margin-top:15px">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.page"
          :page-sizes="[5, 10, 15, 20]" :page-size="params.page_size" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="title" :visible.sync="visible">
      <el-form ref="diaForm" :rules="rules" :model="diaForm">
        <el-form-item clearable prop="alert_type" label="警报类型" :label-width="formLabelWidth">
          <el-input key="diaForm" v-model="diaForm.alert_type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="status" label="警报状态" :label-width="formLabelWidth">
          <el-select key="diaForm" clearable v-model="diaForm.status" placeholder="请选择警报状态">
            <el-option label="已解决" value="resolved"></el-option>
            <el-option label="未解决" value="unresolved"></el-option>
            <el-option label="已忽略" value="ignored"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="description" label="警告描述" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="diaForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { get, Delete, put,post } from '@/utils/request'
import Tinymce from '@/components/Tinymce'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      gData: [],
      form: {},
      params: {
        page: 1,
        page_size: 10
      },
      total: 0,
      title: '警报添加',
      id: 0,
      visible: false,
      diaForm: {},
      formLabelWidth: '120px',
      rules: {
        alert_type: [
          { required: true, message: '请输入警告类型', trigger: 'blue' }
        ],
        status: [
          { required: true, message: '请选择警告状态', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入警告描述', trigger: 'blue' }
        ]
      }
    }
  },
  created() {
    this.loadData()

  },
  methods: {
    toAdd() {
      this.title = '警报添加';
      this.visible = true;
      this.diaForm = {};
    },
    submit() {
      this.$refs.diaForm.validate(async valid => {
        if (valid) {
          let res;
          if (this.title == '警报添加') {
            res = await post(`/accounts/alerts/`, this.diaForm);
            this.$message({
              type: 'success',
              message: res.message
            });

          } else {
            res = await put(`/accounts/alerts/${this.id}/`, this.diaForm);
            this.$message({
              type: 'success',
              message: res.message
            });
          }
          this.visible = false;
          this.loadData();
        }
      })
    },
    toEdit(row) {
      let temp = {};
      this.title = '警报处理';
      this.diaForm = Object.assign(temp, row);
      this.visible = true;
      this.id = row.id;
      delete this.diaForm.id;
      delete this.diaForm.updated_at;
      delete this.diaForm.created_at;
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await Delete(`/accounts/alerts/${id}/`);
        console.log(res);
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.params.page = 1;
        this.loadData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    toSearch() {
      this.params.page = 1;
      this.loadData();
    },
    handleSizeChange(pageSize) {
      this.params.page = 1;
      this.params.page_size = pageSize;
      this.loadData()
    },
    handleCurrentChange(page) {
      this.params.page = page;
      this.loadData()
    },
    async loadData() {
      let temp = {
        ...this.form,
        ...this.params
      }
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key]
        }
      }
      let res = await get('/accounts/alerts/', temp);
      this.gData = res.data;
      this.total = res.pagination.total;
    }
  }
}
</script>
<style scoped></style>